package com.yzd.learnjava.generics;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.yzd.learnjava.generics.coffe.Coffe;
import com.yzd.learnjava.generics.coffe.Latte;
import com.yzd.learnjava.generics.coffe.Mocha;
import com.yzd.learnjava.util.Generator;
import com.yzd.learnjava.util.Print;
import com.yzd.learnjava.util.SimpleQueue;

public class Fill2 {
	public static void main(String[] args) {
		List<Coffe> carrier = new ArrayList<Coffe>();
		Fill2.fill(new AddableCollectionAdapter(carrier), Coffe.class, 3);
		Fill2.fill(Adapter.collectionAdapter(carrier), Latte.class, 2);
		for(Coffe c : carrier) {
			Print.print(c);
		}
		
		Print.print("---------------------");
		
		AddableSimpleQueue<Coffe> coffeQ = new AddableSimpleQueue<Coffe>();
		Fill2.fill(coffeQ, Mocha.class, 3);
		Fill2.fill(coffeQ, Latte.class, 2);
		for(Coffe c : coffeQ) {
			Print.print(c);
		}
	}
	public static <T> void fill(Addable<T> addable, Class<? extends T> classToken, int size) {
		for(int i = 0 ; i < size; i++) {
			try {
				addable.add(classToken.newInstance());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	public static <T> void fill(Addable<T> addable, Generator<T> generator, int size) {
		for(int i = 0 ; i < size ; i++) {
			addable.add(generator.next());
		}
	}
}

interface Addable<T>{
	void add(T t);
}

class AddableCollectionAdapter<T> implements Addable<T>{
	private Collection<T> collection;
	
	public AddableCollectionAdapter(Collection<T> c ) {
		this.collection = c ;
	}
	public void add(T t) {
		collection.add(t);
	}
}

class Adapter {
	public static <T> Addable<T> collectionAdapter(Collection<T> c){
		return new AddableCollectionAdapter(c);
	}
}

class AddableSimpleQueue<T> extends SimpleQueue<T> implements Addable<T>{
	@Override
	public void add(T t) {
		super.add(t);
	}
}
